package com.cqw.commontest.thread.unit3;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-06-07 10:17
 * @Description: ThreadLocal
 */
public class Test1 {

    public static void main(String[] args) {
        new Thread(() -> {
            User user = Test1ThreadLocal.get().get();
            user.setName("张三");
            user.setAge(23);
            System.out.println(user);
        }).start();

        new Thread(() -> {
            User user = Test1ThreadLocal.get().get();
            user.setName("李四");
            user.setAge(24);
            System.out.println(user);
        }).start();

        new Thread(() -> {
            User user = Test1ThreadLocal.get().get();
            user.setName("王五");
            user.setAge(25);
            System.out.println(user);
        }).start();

        new Thread(() -> {
            User user = Test1ThreadLocal.get().get();
            user.setName("赵六");
            user.setAge(26);
            System.out.println(user);
        }).start();
    }

}


class Test1ThreadLocal {
    public static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    public static ThreadLocal<User> get() {
        User user = threadLocal.get();
        if (user == null) {
            user = new User();
            threadLocal.set(user);
        }
        return threadLocal;
    }

}
